package
{
	import flash.utils.ByteArray;

	public class FGS18Processer extends FGS18Client
	{
		/**	socket 透過 proxy 來取得 main 的資訊 */
		private var _proxy:Slot25Standard;

		public function FGS18Processer(r:*, dump:Boolean = false)
		{
			super(dump);
			_proxy = new Slot25Standard(r);
			listen(onEcho);
		}

		public function get main():NewBridge_25LineSlot
		{
			return _proxy.main;
		}

		public function get canvas():Slot25Canvas
		{
			return main.canvas;
		}

		public function get debug():Slot25Debugger
		{
			return main.debug;
		}

		public function get core():Slot25Core
		{
			return main.core;
		}

		public function onEcho(type:String, message:String):void
		{
			switch (type)
			{
				case CONNECT_SUCCESS:
				{
					this.debug.green();
					this.debug.print('連線成功');
					switch (this.debug.autoType)
					{
						case 0:
							break;
						case 1: // 登入工程模式
						{
							this.debug.print('送出工程模式指令 C2S_EngineerLogin:' + this.debug.token);
							C2S_EngineerLogin(this.debug.token);
							this.debug.disable();
							break;
						}
						case 2: // 登入正常模式
						{
							this.debug.print('送出正式模式指令 C2S_LoginGame:' + this.debug.token);
							C2S_LoginGame(this.debug.token);
							break;
						}
						default:
					}
					break;
				}
				case SERVER_CLOSE:
				case CLIENT_CLOSE:
				{
					this.debug.red();
					this.debug.log('連線中斷：' + type + message);
					this.canvas.setTableVisible(false);
					this.main.breakGame(type == SERVER_CLOSE);
					this.resetPosition();
					break;
				}
				case ERROR_ORIGINAL:
				{
					this.debug.red();
					this.debug.log('連線失敗：' + message);
					this.canvas.setTableVisible(false);
					this.main.breakGame();
					this.resetPosition();
					break;
				}
				case DATA_RECEIVE:
				{
					this.debug.log('資料收到：' + message);
					break;
				}
				case DATA_SEND:
				{
					this.debug.log('資料送出：' + message);
					break;
				}
			}
		}

		override public function S2C_DEFPing():void
		{
			//this.debug.print('<< S2C_DEFPing >> C2S_DEFPong.');
			C2S_DEFPong();
		}

		// 工程模式登入，取得 token 後登入遊戲
		override public function S2C_EngineerLogin(option:String):void
		{
			this.debug.print('<< S2C_EngineerLogin，取得 Token：' + option + ' >> C2S_LoginGame 嘗試進入遊戲');
			this.core.token = option;
			C2S_LoginGame(option);
		}

		// 初始化遊戲，解析由 server 傳來的參數
		override public function S2C_InitGame(SID:int, ID:String, Option:String):void
		{
			this.main.tables[this.core.currentLobby].enable();
			this.main.tables[this.core.currentLobby].girlPlayDelay(0.05);
			this.debug.print('<< S2C_InitGame，SID = ' + SID + '，ID = ' + ID + '，Option = ' + Option);
			this.core.initGame(SID.toString(), ID, Option);
		}

		override public function S2C_LinkPlayerInfo(Pos:int, SID:int, Option:String):void
		{
			this.debug.print('<< S2C_LinkPlayerInfo：[' + SID + ',' + Option + ']');
			this.core.initData(Option);
		}

		override public function S2C_StartUp(Option:String):void
		{
			this.debug.print('<< S2C_StartUp：' + Option);
			this.main.tables[this.core.currentLobby].lobby.page = 0;
			this.main.tables[this.core.currentLobby].music = 'Lobby';
			core.playing = true;
			//canvas.removeChild(main.loading.clip);
			main.loading.clip.visible = false;
		}

		override public function S2C_setTableList(Type:int, data:ByteArray):void
		{
			var info:String = loadString2(data);
			//this.debug.print('<< S2C_setTableList：[' + Type + ']');
			this.core.currentLobby = Type;
			this.canvas.setTablePopup(Type);
			this.main.tables[Type].lobby.setPageInfo(info);
		}

		override public function S2C_InitTable(Type:int, Option:String):void
		{
			this.debug.print('<< S2C_InitTable：[' + Type + ',' + Option + ']');
		}

		override public function S2C_IntoTableRet(Type:int, TableNo:int, Ret:int):void
		{
			this.debug.print('<< S2C_IntoTableRet：[' + Type + ',' + TableNo + ',' + Ret + ']');
			this.core.currentLobby = Type;
			this.main.tables[this.core.currentLobby].getInTable(Type, TableNo, Ret);
		}

		override public function S2C_ExitTableRet(Type:int, TableNo:int, Ret:int):void
		{
			this.debug.print('<< S2C_ExitTableRet：[' + Type + ',' + TableNo + ',' + Ret + ']');
			this.main.tables[this.core.currentLobby].getOutTable(Type, TableNo, Ret);
		}

		override public function S2C_LinkBonus(Bonus:int):void
		{
			//this.debug.print('<< S2C_LinkBonus：[' + Bonus + ']');
			this.core.jackpot = Bonus;
		}
		private var _confirmNewType:Number  = 0;
		private var _confirmNewTable:Number = 0;
		private var _confirmOldType:Number  = 0;
		private var _confirmOldTable:Number = 0;

		override public function S2C_ConfirmTable(NewType:int, NewTable:int, OldType:int, OldTable:int):void
		{
			this.debug.print('<< S2C_ConfirmTable：[' + NewType + ',' + NewTable + ']');
			_confirmNewType = NewType;
			_confirmNewTable = NewTable;
			_confirmOldType = OldType;
			_confirmOldTable = OldTable;
			var content:String = '您目前已有保留【' + this.core.barname[OldType] + '[' + this.core.format(String(OldTable), '000') + ']】，' + "\n" + '是否要進保留台？';
			//'要返回機台接續遊戲請按【返回拉霸】，' + "\n" +
			//'如要取消原保留機台請按【取消保留】。';
			this.main.tables[this.core.currentLobby].alert.addAlready(content, onConfirmOld, onConfirmNew);
		}

		public function onConfirmOld():void
		{
			this.debug.print('>> C2S_ConfirmTable：[' + 0 + ',' + _confirmOldType + ',' + _confirmOldTable + ']');
			this.core.currentLobby = _confirmOldType;
			this.canvas.setTablePopup(_confirmOldType);
			this.main.tables[_confirmOldType].music = '';
			this.main.tables[_confirmNewType].music = '';
			C2S_ConfirmTable(0, _confirmOldType, _confirmOldTable);
		}

		public function onConfirmNew():void
		{
			this.debug.print('>> C2S_ConfirmTable：[' + 1 + ',' + _confirmNewType + ',' + _confirmNewTable + ']');
			this.core.currentLobby = _confirmNewType;
			this.canvas.setTablePopup(_confirmNewType);
			this.main.tables[_confirmOldType].music = '';
			this.main.tables[_confirmNewType].music = '';
			C2S_ConfirmTable(1, _confirmNewType, _confirmNewTable);
		}

		override public function S2C_RunBAR(BarNo:Array, LineSymbols:Array, LineCount:Array, HeartCount:int, WinPoint:int, NowPoint:int):void
		{
			this.debug.print('<< S2C_RunBAR：[' + WinPoint + ',' + NowPoint + ']');
			this.main.tables[this.core.currentLobby].machine.onResult(BarNo, LineSymbols, LineCount, HeartCount, WinPoint, NowPoint);
		}

		override public function S2C_SubGame1RunBAR(SelectCount:int, Select:Array, Rate:int, PlayCount:int, BarNo:Array, LineSymbols:Array, LineCount:Array, WinPoint:Array, NowPoint:Array):void
		{
			this.debug.print('<< S2C_SubGame1RunBAR：[' + PlayCount + ']');
			var data:Object = {SelectCount: SelectCount, Select: Select, Rate: Rate, PlayCount: PlayCount, BarNo: BarNo, LineSymbols: LineSymbols, LineCount: LineCount,
					WinPoint: WinPoint, NowPoint: NowPoint};
			this.main.tables[this.core.currentLobby].machine.saveFreeGirl(data);
		}

		override public function S2C_SubGame2RunBAR(cards:Array, gold:Array, card:int, position:int, total:int):void
		{
			this.debug.print('<< S2C_SubGame2RunBAR：[' + total + ',' + this.core.currentLobby + ']');
			var data:Object = {Cards: cards, Gold: gold, Madel: card, Position: position, Total: total};
			this.main.tables[this.core.currentLobby].machine.saveFreeJungle(data);
		}

		override public function S2C_Talk(SID:int, msg:String):void
		{
			this.debug.print('<< S2C_Talk：' + msg);
			this.core.addTalking(msg);
		}

		override public function S2C_SysMessage(msg:String):void
		{
			this.debug.print('<< S2C_SysMessage：' + msg);
			trace('<< S2C_SysMessage：' + msg);
			this.core.delayMessage(msg);
		}

		override public function S2C_AwardMessage(TableType:int, TableNo:int, ID:String, Name:String, Msg:String):void
		{
			this.debug.print('<< S2C_AwardMessage：' + Msg);
			trace('<< S2C_AwardMessage：' + Msg);
			this.core.delayAward(TableType, TableNo, ID, Name, Msg);
		}

		override public function S2C_SomeoneGetLinkBonus(TableType:int, TableNo:int, ID:String, Name:String, BonusType:int, Point:int):void
		{
			this.debug.print('<< S2C_SomeoneGetLinkBonus：' + Point);
			trace('<< S2C_SomeoneGetLinkBonus：' + Point);
			this.core.delayBonus(TableType, TableNo, ID, Name, BonusType, Point);
		}

		override public function S2C_EventOption(EventNo:int, Option:int):void
		{
			this.debug.print('<< S2C_EventOption：' + EventNo + ',' + Option);
			this.core.delayEvent(EventNo, Option);
		}
	}
}
